package com.example.myalertdialog;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;

public class MainActivity extends AppCompatActivity {

    private static final String TAG ="leo";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void leoClick(View view) {
        //View dialogBiew= getLayoutInflater().inflate(R.layout.dialog_view,null);
        View popupView = LayoutInflater.from(this).inflate(R.layout.dialog_view ,null);
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        /*builder.setIcon(R.mipmap.ic_launcher)
                .setTitle("这个是标题")
                .setMessage("这个是内容小老弟")
                .setView(dialogBiew)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.e(TAG,"点击了确定");

                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.e(TAG,"点击了取消");
                    }
                })
                .setNeutralButton("中间按钮", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.e(TAG,"点击了中间");
                    }
                })
                .create()
                .show();
        //getWindow().setBackgroundDrawableResource(R.drawable.tp_kuang);
        AlertDialog dialog = new AlertDialog.Builder(this).setTitle("title")

                .setMessage("message").create();

        Window window = dialog.getWindow();

        window.setGravity(Gravity.TOP);  */ //window.setGravity(Gravity.BOTTOM);
       // showDialog();
        //dialog.show();

       /* PopupWindow popupWindow=new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
                ,true);//ViewGroup.LayoutParams.WRAP_CONTENT相当于在xml中配置长宽,最后一个参数表示可以在空白出点击退出
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.dialogbj));//设置背景色
        popupWindow.showAsDropDown(MainActivity.this.getWindow().getDecorView(),0,0); //最后两个参数x。y用来指定view的位置，不设置默认显示在下方*/
        View dialogBiew= getLayoutInflater().inflate(R.layout.dialog_viewa,null);
        Mydialog myAlertDialog=new Mydialog(this,R.style.dialog);
        myAlertDialog.setContentView(dialogBiew);
        myAlertDialog.show();


    }
   /* * 设置对话框位置，竖屏要调整位置
     * @param dialog
     */
    private void setDialogPos(Dialog dialog) {
        Log.e("setDialogPos","setDialogPos位置");
        if (true||false) { // 竖屏，重新设置对话框位置
            Window dialogWindow = dialog.getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            dialogWindow.setGravity(Gravity.TOP);
           /* if (dialog instanceof PublicDialog) {
                lp.y = 100; // 新位置Y坐标
            } else {
                lp.y = 200;
            }*/
            dialogWindow.setAttributes(lp);
        }
    }
    private void showDialog(){
        View dialogBiew= getLayoutInflater().inflate(R.layout.dialog_viewa,null);

        AlertDialog dialog=new AlertDialog.Builder(this,R.style.dialog)
                .setView(dialogBiew)
                .create();
        dialog.show();
       /* WindowManager.LayoutParams params =

                dialog.getWindow().getAttributes();

        params.width = 496;

        params.height = 276;

        dialog.getWindow().setAttributes(params);*/
        //	.show();
        dialogBiew.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }
}